package practise.alogrithm.hundredInterviewQuestions;

import practise.alogrithm.hundredInterviewQuestions.Utils.LinkedListBuilder;
import practise.alogrithm.hundredInterviewQuestions.Utils.ListNode;

/**
 * Created by angela on 6/8/2014.
 */
public class DeleteNodeFromLinkedListInLog1 {
    public static ListNode deleteNode(ListNode listNode, int nodeValue){
        ListNode tempNode = new ListNode();
        tempNode.setNext(listNode);
        if(tempNode.getNext().getValue()==nodeValue){
            listNode = listNode.getNext();
        }
        while(tempNode.getNext().getNext()!=null){
            if(tempNode.getNext().getValue()==nodeValue){
                tempNode.setNext(tempNode.getNext().getNext());
            }
            tempNode = tempNode.getNext();
        }

        if(tempNode.getNext().getValue()==nodeValue){
            tempNode.setNext(null);
        }
        return listNode;
    }

    public static void main(String[] args){
        ListNode listNode = LinkedListBuilder.buildLinkedListFromArray(new int[]{1,2,3,4,5,6,7,8,9});
        ListNode newNode = deleteNode(listNode,1);
        for(;newNode!=null;newNode=newNode.getNext()){
            System.out.print(newNode.getValue()+"=>");
        }

        System.out.println();

         newNode = deleteNode(listNode,0);
        for(;newNode!=null;newNode=newNode.getNext()){
            System.out.print(newNode.getValue()+"=>");
        }

    }
}
